package com.lcxedu.notebook.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcxedu.notebook.entity.Event;
import com.lcxedu.notebook.entity.EventWithCategory;
import com.lcxedu.notebook.service.EventMapperService;
import com.lcxedu.notebook.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Title: EventController
 * @Author: 李晨曦
 * @Date: 2025/6/11
 * 好好看 好好学 都能学会 都能学好
 * 我亦无他 唯手熟尔
 */

@Controller
public class EventController {
    @Resource
    private EventMapperService eventMapperService;

    @GetMapping("/user/{userId}")
    @ResponseBody
    public Result getAllEventsWithCategoriesByUserId(@PathVariable("userId") Integer userId) {
        List<EventWithCategory> events = eventMapperService.getAllEventsWithCategoriesByUserId(userId);

        return Result.success(events);
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Result deleteEvent(@PathVariable("id") Integer id) {
        eventMapperService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/edit")
    @ResponseBody
    public Result updateEvent(@RequestBody Event event) {
        int i = eventMapperService.updateByPrimaryKeySelective(event);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("400", "修改错误");
    }

    @PostMapping("/addEvent")
    @ResponseBody
    public Result addEvent(@RequestBody Event event) {
        int i = eventMapperService.insertSelective(event);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("400", "添加失败");
    }

    //条件分页查询
    @RequestMapping("/ConditionPage1")
    @ResponseBody
    public Result listALLByConditionPages(@RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "5") Integer pageSize,
                                            @RequestParam(defaultValue = "userId") Integer userId,
                                            @RequestParam(defaultValue = "") String search) {

        PageHelper.startPage(pageNum, pageSize);

        List<EventWithCategory> events = eventMapperService.getAllEventsWithCategoriesByPage(userId, search);

        PageInfo pageInfo = new PageInfo(events, pageSize);

        return Result.success(pageInfo);
    }

    // 条件分页查询
    @GetMapping("/ConditionPage")
    @ResponseBody
    public Result listByConditionPages(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "8") Integer pageSize,
            @RequestParam Integer userId,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String status
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<EventWithCategory> events = eventMapperService.getAllEventsWithCategoriesByCondition(userId, title, categoryId, level, status);
        PageInfo pageInfo = new PageInfo(events, pageSize);
        return Result.success(pageInfo);
    }
}
